<template>
  <div style="height:100%;overflow:hidden;">
      <el-tabs v-model="tabvalue" style="height:100%;" type="border-card" ref="tabs" class="Tabundefined RMTab">
          <el-tab-pane label="pdf报告" name="1" key="1">
              <el-upload ref="uploadDat" action="" :auto-upload="false" accept=".pdf" :on-change="fileChange" :show-file-list="false">
                  <el-button size="small" plain>选择文件</el-button>
              </el-upload>
              <div style="height: calc(100% - 60px)">
                  <!-- <el-image :src="image" style="width:100%;height:100%"></el-image> -->
                  <iframe :src="image" style="width:100%;height:100%"></iframe>
                  <!-- <div style="width:100%;height:100%" v-html="image"></div> -->
                  <!-- <pdf v-for="i in pdfPages" :key="i" :src="url" :page="i"></pdf> -->
              </div>
          </el-tab-pane>
          <el-tab-pane label="结果数据" name="2" key="2">
              <vxe-table :data.sync="tableDataResult" :sort-config="{trigger: 'cell'}" :select-config="{ tigger: 'row' }" :height="h" highlight-current-row ref="xTableResult" resizable border show-header-overflow="tooltip" show-overflow="tooltip" header-align="center" :loading="loadingResult" size="mini" :width="w" config-record-key="ResultTable" status-msg-group="none" class="ResultTable">
                  <vxe-table-column :field="items.field" :title="items.title" :width="items.width" v-for="(items) in tableResultColumn" v-bind:key="items.field" :align="items.field=='result_str'&&Options.resultstralignmentright>0?'right':'left'" header-align="center">
                      <template v-slot="{ row }" v-if="items.field=='alertflag'||items.field=='redo_flag'||items.field=='modify_flag'">
                          <div v-if="items.field=='alertflag'&&row.alertflag>0" v-bind:style="{background:Options.alertflagcolor?Options.alertflagcolor:'red',height:'18px'}"></div>
                          <div v-else-if="items.field=='modify_flag'&&row.modify_flag>0" v-bind:style="{background:Options.modifyflagcolor?Options.modifyflagcolor:'#FFFFFF',height:'18px'}"></div>
                          <div v-else-if="items.field=='redo_flag'&&row.redo_flag == '2'" v-bind:style="{background:'rgb(00, 255, 00)',height:'18px'}"></div>
                          <div v-else-if="items.field=='redo_flag'&&(row.redo_flag == '1' || row.redo_flag == 'M')" v-bind:style="{background:'yellow',height:'18px'}"></div>
                          <div v-else></div>
                      </template>
                      <template v-slot="{ row }" v-else-if="items.field == 'rpt_itemname'">
                          <div :style="ResultClassName(row)">
                              <span>{{row.rpt_itemname}}</span>
                          </div>
                      </template>
                      <template v-slot="{ row }" v-else-if="items.field=='result_str'">
                          <div :style="ResultClass(row)">
                              <span v-if="Options.resultstrbold=='1'" style="font-weight:bold">{{row.result_str}}</span>
                              <span v-else>{{row.result_str}}</span>
                          </div>
                      </template>
                      <template v-slot="{ row }" v-else-if="items.field == 'cp_lastresult'">
                          <div :style="ResultClass(row, 'last')">
                              <span v-if="Options.resultstrbold=='1'" style="font-weight:bold">{{row.cp_lastresult}}</span>
                              <span v-else>{{row.cp_lastresult}}</span>
                          </div>
                      </template>
                      <template v-slot="{ row }" v-else-if="items.field=='result_txt'">
                          <pre style="padding: 1px;white-space: pre-wrap;">{{ row.result_txt }}</pre>
                          <!-- <el-input v-if="editflag" v-model="row.result_txt" type="textarea" autosize class="txtClass" @dblclick.native="tempRptItem=row,dialogResultTxtVisible = true,ResultTxt = row.result_txt"></el-input> -->
                      </template>
                  </vxe-table-column>
              </vxe-table>
          </el-tab-pane>
      </el-tabs>

  </div>
</template>
<script>
import { Promise } from "q";
import XE from "xe-utils";
// import pdf from "vue-pdf";
export default {
  // components: {
  //     pdf
  // },
  props: ["PatientInfo", "rptunitid", "pOptions", "h", "w"],
  name: "LIS310",
  data() {
      return {
          DelIndex: -1,

          myrptunitid: this.rptunitid,
          tableDataResult: [],
          tableResultColumn: [],
          loadingResult: false,

          editflag: true,
          Options: this.pOptions,
          Refreshflag: false,
          // url: "",
          // pdfPages: 1,
          image: "",
          tabvalue: "1"
      };
  },
  components: {},
  created() {
      this.InitPageData();
  },
  mounted() {
      // 监听body上的点击
      document.querySelector("body").addEventListener("click", this.closeMenu);
  },
  beforeDestroy() {
      // 移除监听
      document.querySelector("body").removeEventListener("click", this.closeMenu);
  },
  updated() {},
  methods: {
      InitPageData() {},
      //上方按钮功能

      //新增
      ResultAdd() {
          return new Promise((s, f) => {
              s();
          });
      },
      ResultAddAsync() {
          this.ResultAdd().then(() => {
              this.$emit("change");
          });
      },
      //删除
      ResultDel() {
          this.$table.DelRecordSelect(this.$refs.xTableResult, this.DelIndex, this);
          var row = this.$refs.xTableResult.getCurrentRow();
          this.$refs.xTableResult.setActiveCell(row, "rpt_itemname");
      },
      ResultDelAsync() {
          new Promise(resolve => {
              this.ResultDel();
              resolve();
          }).then(() => {
              this.$emit("change");
              this.$refs.xTableResult.$refs.tableBody.$children[0].focus();
          });
      },
      ClearNoDoItems() {
          return new Promise((s, f) => {
              if (this.editflag) {
                  var delrow = [];
                  this.$refs.xTableResult.tableFullData.forEach(t => {
                      if (!t.result_str || t.result_str == "" || t.result_str == "未做") {
                          delrow.push(t);
                      }
                  });
                  delrow.forEach(element => {
                      this.$refs.xTableResult.remove(element);
                  });
                  // var tmplis = this.tableDataResult.filter(t => t.result_str && t.result_str != "" && t.result_str != "未做");
                  // this.tableDataResult = tmplis;
                  s();
              } else {
                  f();
              }
          });
      },

      RefreshOptions(p) {
          this.Options = p;
          //console.log("option",p);
          this.tableResultColumn = this.Options.rCols;
      },
      ClearTableDate() {
          return new Promise((s, f) => {
              this.tableDataResult = [];
              this.editflag = true;
              s();
          });
      },
      QueryResult(rptinfo) {
          return Promise((s, f) => {
              this.Refreshflag = true;
              let reportid = rptinfo.reportid;
              if (rptinfo.rechkflag == "9" || (this.Options.onlymodifymyreport == "1" && $tool.getSession(this.$tool.loginuserno) != rptinfo["report_user"]) || (rptinfo.barcode && rptinfo.barcode.indexOf("-退") > -1)) {
                  this.editflag = false;
              } else {
                  this.editflag = true;
              }
              let logcontent = "";
              logcontent += XE.toDateString(new Date(), "yyyy-MM-dd HH:mm:ss.S") + " :开始查询报告: " + reportid + "\r\n";
              //this.$pub.Log("开始查询报告:" + reportid);
              //每个报告都做一下，防止勾选了本组报告单元，获取显示PDF报告选项放到后台去
              if (reportid) {
                  this.queryreportpdf(reportid);
              } else {
                  this.queryreportpdf("no");
              }
              this.$tool
                  .request({
                      url: "LIS/LIS3/LIS312",
                      method: "post",
                      data: {
                          action: "queryresultpdf",
                          reportid: reportid,
                          rptunitid: rptinfo.rptunitid,
                          sampleno: rptinfo.sampleno,
                          sampledate: rptinfo.sampledate
                      }
                  })
                  .then(res => {
                      logcontent += XE.toDateString(new Date(), "yyyy-MM-dd HH:mm:ss.S") + " :result开始查询报告结束: " + reportid + "\r\n";
                      //this.$pub.Log("result开始查询报告结束:" + reportid);
                      this.tableDataResult = res.vxtab.Data;
                      //console.log("res", res);
                      // if (res.imgstr) {
                      //     // this.image = res.imgstr;
                      //     //let url = window.URL.createObjectURL(res.pdfdt[0].filehex);
                      //     //console.log("length", res.imgstr.length);
                      //     var binaryData = [];
                      //     binaryData.push(res.imgstr);
                      //     //this.image = res.imgstr;
                      //     //var bytearr = this.Str2Bytes(res.imgstr);
                      //     let blob = new Blob(binaryData, {
                      //         type: "application/pdf"
                      //     });
                      //     let objectUrl = window.URL.createObjectURL(blob);
                      //     this.image = objectUrl;
                      // } else {
                      //     this.image = "";
                      // }
                      this.QueryImagePDF(reportid);
                      //this.$refs.xTableResult.clearActived();
                      //rptinfo.redo_flag = res.redo_flag;
                      //rptinfo.alter_flag = res.alter_flag;
                      this.loadingResult = false;
                      this.Refreshflag = false;
                      var returndata = { res: res.rptinfo, redo: res.redo, history: res.history };
                      //不在前台做待审核状态判断了
                      //var renum = 1;
                      //if (res.vxtab.Data && res.vxtab.Data.length > 0) renum = this.tableDataResult.filter(item => item.result_str == "未做").length;
                      //报告处理-导入在父组件没有PatChange方法，这里就先注释掉，后面需要再说
                      //this.$emit("PatChange", res.rptinfo, res.prechkflag, "");
                      logcontent += XE.toDateString(new Date(), "yyyy-MM-dd HH:mm:ss.S") + " :result开始查询报告赋值结束: " + reportid + "\r\n";
                      this.$pub.Log(logcontent);
                      s(returndata);
                  })
                  .catch(error => {
                      this.$message.error("错误:" + error);
                      this.Refreshflag = false;
                      f();
                  });
          });
      },
      queryreportpdf(reportid) {
          if(reportid != "no"){
              this.$tool
                  .request({
                      url: "LIS/LIS3/LIS310",
                      method: "post",
                      data: {
                          action: "queryreportpdf",
                          reportid: reportid,
                      }
                  })
                  .then(res => {
                      if(res.flag == -1) {//勾选本组报告后选择的报告不属于开启悬浮窗的报告单元
                          this.$emit("ShowReportPDF", false, "", -1);
                      } else {
                          this.$emit("ShowReportPDF", true, res.url, res.flag);
                      }
                  })
                  .catch(e => {
                      this.$alert("报告pdf显示出错！", "错误", {
                          confirmButtonText: "确定"
                      });
                  });
          } else {
              //无报告的情况下按当前报告单元来决定开悬浮窗还是不开
              if(this.Options.showpdfreport == "1"){
                  this.$emit("ShowReportPDF", true, "", 0);
              } else {
                  this.$emit("ShowReportPDF", false, "", 0);
              }
          }
      },
      //病历号改变的逻辑
      selectchange(item, row) {
          // console.log("111",val);
          row.result_str = item.textvalue;
          row.result_flag = item.result_flag;
          this.$refs.xTableResult.clearActived();
      },
      fileChange(file, fileList) {
          //this.form.file = file.raw;
          console.log("file", file.raw);
          this.uploadFile(file.raw);
      },
      uploadFile(file) {
          this.$emit("uploadFile", file);
      },
      Str2Bytes(str) {
          var pos = 0;
          var len = str.length;
          if (len % 2 != 0) {
              return null;
          }
          len /= 2;
          var hexA = new Array();
          for (var i = 0; i < len; i++) {
              var s = str.substr(pos, 2);
              var v = parseInt(s, 16);
              hexA.push(v);
              pos += 2;
          }
          return hexA;
      },
      QueryImagePDF(reportid) {
          if (reportid) {
              this.$tool
                  .request({
                      url: "/ucent/downloadfile",
                      method: "post",
                      responseType: "blob",
                      data: {
                          prgno: "LIS312",
                          reportid: reportid
                      }
                  })
                  .then(res => {
                      let blob = new Blob([res], {
                          type: "application/pdf"
                      });
                      let objectUrl = window.URL.createObjectURL(blob);
                      this.image = objectUrl;
                  });
          } else {
              this.image = "";
          }
      },
      ResultClass(row, type) {
          var stylestr = "padding:0 3px;";
          if (this.Options.resultcolor_mode == "1") stylestr += "color:";
          else stylestr += "background-color:";
          var flag = row.result_flag;
          if (type && type == "last") flag = row.cp_lastresultflag;
          switch (flag) {
              case "M":
                  stylestr += this.Options.resultcolor_m;
                  break;
              case "H":
                  stylestr += this.Options.resultcolor_h;
                  break;
              case "L":
                  stylestr += this.Options.resultcolor_l;
                  break;
              case "P":
                  stylestr += this.Options.resultcolor_p;
                  break;
              case "W":
                  stylestr += this.Options.resultcolor_w;
                  break;
              case "E":
                  stylestr += this.Options.resultcolor_e;
                  break;
              case "Q":
                  stylestr += this.Options.resultcolor_r;
                  break;
              default:
                  stylestr += this.Options.resultcolor_m;
                  break;
          }
          if (row.result_str == "未做") {
              stylestr += ";color: #0095D9";
          }
          return stylestr;
      },
      ResultClassName(row) {
          var stylestr = "padding:0 3px;";
          if (row.alertflag > 0) stylestr += "background-color:" + (this.Options.alertflagcolor ? this.Options.alertflagcolor : "red") + ";";
          if (row.linkitemfalg == "1") stylestr += "text-decoration:underline;";
          return stylestr;
      }
  },
  watch: {
      rptunitid(val) {
          this.myrptunitid = val;
      }
  }
};
</script>

<style scoped>
/deep/.disabled-cell {
  background-color: #f7f7f7;
}
/deep/.warn-high {
  background-color: #ffc0c0;
}
/deep/.warn-low {
  background-color: #c0c0ff;
}
/deep/.danger {
  background-color: #ff0000;
}
/deep/.paddingcolumn {
  padding: 0 3px;
}
/deep/.fontbold {
  font-weight: bold;
}
/deep/.el-image {
  z-index: 2007;
}
/deep/.vxe-body--row .el-input--small .el-input__inner {
  height: 18px !important;
  line-height: 18px !important;
}
/deep/.middle-left .el-checkbox__input.is-checked + .el-checkbox__label {
  color: red;
}
/deep/.dialogdanger .el-textarea.is-disabled .el-textarea__inner {
  color: red;
}
.menu_style {
  position: absolute;
  width: 150px;
  background-color: #fff;
  border-radius: 2px;
  box-shadow: 2px 2px 14px #d0d0d0;
  z-index: 2008;
}
.menu_style > ul > li {
  text-indent: 25px;
  height: 38px;
  line-height: 38px;
  border-bottom: 1px dashed #f0f0f0;
  cursor: pointer;
}
.menu_style > ul > li:hover {
  background: #e0eeff;
}
.dangerPatInfo tr td:nth-child(even) {
  width: 40px;
  border-bottom: 1px solid black;
}

table tr td:nth-child(odd) {
  text-align: right;
  white-space: nowrap;
}
table tr td:nth-child(even) {
  text-align: left;
}
table tr td:nth-child(1) {
  width: 80px;
}
::v-deep .ResultTable.vxe-table .vxe-cell {
  padding: 0;
  line-height: 18px;
}
/deep/.paddingcolumn .vxe-cell {
  padding: 0 3px;
}
.el_main {
  padding: 0 0;
}
::v-deep .ResultTable .vxe-table--body td {
  height: 18px !important;
}
::v-deep .txtClass .el-textarea__inner {
  padding: 0;
}
</style>
